import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/ute/uteped_config.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/generated/locales.g.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';

import 'app_remind_state.dart';

class AppRemindLogic extends BaseController {
  final AppRemindState state = AppRemindState();
  late UTEPedConfig _utePedConfig;

  @override
  void onInit() {
    super.onInit();
    _utePedConfig = UTEPedDevice.instance.getUteConfig();
    _utePedConfig.checkNotificationEnable();
  }

  @override
  Future<void> onReady() async {
    if (Platform.isAndroid) {
      state.isNotificationEnable = await _utePedConfig.isNotificationEnable();
      if (!state.isNotificationEnable) {
        showPermissionDialog(LocaleKeys.notification_permission_tips.ts, callback: ()=>{
          _utePedConfig.toNotificationPage()
        });
      }
    }
    _loadInstalledApps();
    super.onReady();
  }

  @override
  Future<void> onResumed() async {
    if (Platform.isAndroid) {
      state.isNotificationEnable = await _utePedConfig.isNotificationEnable();
    }
    _loadInstalledApps();
    super.onResumed();
  }

  Future<List<dynamic>> loadApps() async {
    try {
      var content = await rootBundle.loadString('assets/data/app_remind.json');
      return jsonDecode(content);
    } catch (e) {
      return [];
    }
  }

  void _loadInstalledApps() {
    showLoading();
    loadApps().then((value) => {
      _updateApp(value)
    });
  }
  
  void _updateApp(List<dynamic> appList) {
    for (var app in appList) {
      var packageName = app[UTEPedConst.keyPackageName];
      bool isChecked = read(packageName, defaultValue: false);
      if (Platform.isAndroid && !state.isNotificationEnable) {
        state.appRemindValue[packageName] = false;
      }
      else {
        state.appRemindValue[packageName] = isChecked;
      }
    }

    state.installedApps = appList;
    update();
    cancelLoading();
  }

  Future<void> switchOnChange(String id, bool value) async {
    if (Platform.isAndroid) {
      if (!state.isNotificationEnable) {
        showPermissionDialog(LocaleKeys.notification_permission_tips.ts, callback: ()=>{
          _utePedConfig.toNotificationPage()
        });

        return;
      }
    }
    vRemoteReqAsync(UTEPedConst.methodSetNotificationEnable, {UTEPedConst.keyEnable: value, UTEPedConst.keyPackage: id});
    update([id]);

    save(id, value);
  }


  @override
  String getRemotePath() {
    return UTEPedConst.pathCommon;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.appRemindValue[UTEPedConst.appRemindOtherId] = read(UTEPedConst.appRemindOtherId, defaultValue: false);
  }

  @override
  void onRemoteEvent(event, method) {
  }
}
